package com.example.quartzdemo.QuartzTwo;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * TODO   第二个定时
 *
 * @ClassName QuartzTwo
 * @Author xiaojiang
 * @Date 2021/11/12 下午 16:29
 */
@Configuration
public class QuartzTwo {

    @Bean
    public JobDetail QuartzTwoJobDetail() {
        return JobBuilder.newJob(QuartzTwoJob.class)  // 这里关联对应的 QuartzJobBean
                .withIdentity("QuartzTwoJobTask")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger QuartzTwoJobTrigger() {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(QuartzTwoJobDetail())
                .withIdentity("QuartzTwoJobTask")
                .withSchedule(cronScheduleBuilder)
                .build();
    }
}

    class QuartzTwoJob  extends QuartzJobBean {

        @Override
        protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            //获取JobDetail中关联的数据
            System.out.println("这是第二个定时");
            JobDetail jobDetail = jobExecutionContext.getJobDetail();
            System.out.println(jobDetail.toString());
            System.out.println(jobDetail.getJobBuilder().withIdentity("StatsLogTask"));
            System.out.println(jobDetail.getJobBuilder().toString());
            System.out.println(jobDetail.getJobClass());
            System.out.println("这是第二个job");
            System.out.println(jobExecutionContext.getTrigger());
            System.out.println(jobExecutionContext.getFireInstanceId());
//        String msg = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("msg");
//        System.out.println("current time :"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "---" + msg);

        }

}
